/**
*  Copyright 2009-2014 by WWW.KNET.CN  All rights reserved.
*/
package com.domain.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.domain.model.CloudWebDish;
import com.domain.model.CloudWebMessage;
import com.domain.model.Dict;
import com.domain.model.SysNode;
import com.domain.service.CloudWebDishService;
import com.domain.service.CloudWebMessageService;
import com.domain.service.DictService;
import com.domain.service.SysNodeService;

/**
 * TODO(这里用一句话描述这个类的作用)
 * @author liujie
 * @date 2015年4月28日 下午6:16:07
 * 
 */
@Controller
@RequestMapping("index/")
public class IndexController extends SuperController{
	@Resource
	private CloudWebMessageService cloudWebMessageService;
	@Resource
	private CloudWebDishService cloudWebDishService;
	@Resource
	private SysNodeService sysNodeService;
	@Resource
	private DictService dictService;
	
	//导航
	@RequestMapping("/nav.html")
	@ResponseBody
	public Map<String, Object> getNavNode(HttpServletRequest request,String porCode,HttpServletResponse response){
		Map<String, Object> map=new HashMap<>();
		SysNode node=sysNodeService.getNodeByCode(porCode);
		if (node !=null  && !node.getParentsNodeId().equals("0")) {
			map.put("cNode",node);
			SysNode parentNode=sysNodeService.getNodeByCode(node.getParentsNodeId());
			if (parentNode !=null) {
				map.put("pNode", parentNode);
			}
		}
	   return map;
	}
	
	@RequestMapping("/index.html")
	public String index(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		getMenu(request); //菜单
		rollActivity(model); //首页中间滚动图片
		indexVideo(model); //视频显示
		getNews(model);//新闻
		getDish(model);//特色菜
		storeRoll(request); //各店分布信息滚动，联系我们
		generalActivity(model);//优惠活动
		storeAddress(model);//各门店图片
		moveActivity(model); //首页漂浮广告
		getActiv(model); //微信微博活动
		QQinfo(model);//QQ联系我们
	    return "index";
    }
	private void getMenu(HttpServletRequest request){
		
		List<SysNode> menu=sysNodeService.getMenu("10");
		request.getSession().setAttribute("menu", menu);
		Dict dict=dictService.getDictByCode("welcome");
		request.getSession().setAttribute("welcome", dict);
	}
	//企业新闻带图片的最新五条 3002
	private  void getNews(ModelMap model){
		String porCode="3002";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebMessage webMessage = new CloudWebMessage();
		webMessage.setPorCode(porCode);
		webMessage.setIfDelete(0l);
		webMessage.setMesStatus(1l);
		paramMap.put(webMessage.key, webMessage);
		paramMap.put("mesCccessoryNot", "y");//查询附件路径不为空的数据
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,8);
		paramMap.put("orderBy", "mes_status desc,mes_when desc");
		List<CloudWebMessage> newsList=cloudWebMessageService.getListPage(paramMap);
		List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
		model.addAttribute("newsList", newsList);
		for (CloudWebMessage obj : newsList) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("mesName",obj.getMesName());
			map.put("mesAccessory", obj.getMesAccessory());
			map.put("porCode", obj.getPorCode());
			map.put("id", obj.getId());
			list.add(map);
		}
		String array = JSONArray.toJSONString(list);
		model.put("newsArray",array);
	}
	private void getDish(ModelMap model){
		String porCode="2003";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebDish webDish = new CloudWebDish();
		webDish.setPorCode(porCode);
		webDish.setIfDelete(0l);
		webDish.setDishStatus(1l);
		paramMap.put(webDish.key, webDish);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,10);
		paramMap.put("orderBy", "dish_status desc,dish_when desc");
		List<CloudWebDish> dishList = cloudWebDishService.getListPage(paramMap);
		model.addAttribute("dishList", dishList);
		reqInfo(model, porCode, "news");
	}
	//微博微信等活动
	private void getActiv(ModelMap model){
		String porCode="1102";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebMessage webMessage = new CloudWebMessage();
		webMessage.setPorCode(porCode);
		webMessage.setIfDelete(0l);
		webMessage.setMesIfreview(1l);
		paramMap.put(webMessage.key, webMessage);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,8);
		paramMap.put("orderBy", "mes_status desc,mes_when desc");
		List<CloudWebMessage> newsList=cloudWebMessageService.getListPage(paramMap);
		model.addAttribute("wxList", newsList);
	}
	//各店地址
	private void storeRoll(HttpServletRequest request){
		String porCode="6003";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebDish webDish = new CloudWebDish();
		webDish.setPorCode(porCode);
		webDish.setIfDelete(0l);
		paramMap.put(webDish.key, webDish);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,Integer.MAX_VALUE);
		paramMap.put("orderBy", "dish_status desc,dish_when desc");
		List<CloudWebDish> dishList = cloudWebDishService.getListPage(paramMap);
		HttpSession session =request.getSession();
		if(null != session.getAttribute("storeList")){
			session.removeAttribute("storeList");
			session.setAttribute("storeList", dishList);
		}else
			session.setAttribute("storeList", dishList);
		
		
		  Map<String, Object> map = new HashMap<String, Object>();
		   CloudWebMessage webMessage = new CloudWebMessage();
			webMessage.setPorCode("4004");
			webMessage.setIfDelete(0l);
			map.put(webMessage.key, webMessage);
			map.put(PAGE_NAME, 1);
			map.put(PAGESIZE_NAME,1);
			map.put("orderBy", "mes_status desc,mes_when desc");
			List<CloudWebMessage> msgList=cloudWebMessageService.getListPage(map);
			if (null != msgList && msgList.size()>0) {
				CloudWebMessage msg=msgList.get(0);
				if(null != session.getAttribute("our")){
					session.removeAttribute("our");
					session.setAttribute("our", msg);
				}else session.setAttribute("our", msg);
			}
			
	}
	//首页滚动大图
	private void rollActivity(ModelMap model){
		String porCode="1103";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebMessage webMessage = new CloudWebMessage();
		webMessage.setPorCode(porCode);
		webMessage.setIfDelete(0l);
		webMessage.setMesIfreview(1l);
		paramMap.put(webMessage.key, webMessage);
		paramMap.put("mesCccessoryNot", "y");//查询附件路径不为空的数据
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,5);
		paramMap.put("orderBy", "mes_status desc,mes_when desc");
		List<CloudWebMessage> newsList=cloudWebMessageService.getListPage(paramMap);
		model.addAttribute("activityRollList", newsList);
	}
	
	//优惠活动
	private void generalActivity(ModelMap model){
		String porCode="1103";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebMessage webMessage = new CloudWebMessage();
		webMessage.setPorCode(porCode);
		webMessage.setIfDelete(0l);
		paramMap.put(webMessage.key, webMessage);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,4);
		paramMap.put("orderBy", "mes_status desc,mes_when desc");
		List<CloudWebMessage> newsList=cloudWebMessageService.getListPage(paramMap);
		model.addAttribute("activityList", newsList);
	}
	//店面图片
	private void storeAddress(ModelMap model){
		String porCode="6002";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebDish webDish = new CloudWebDish();
		webDish.setPorCode(porCode);
		webDish.setIfDelete(0l);
		webDish.setDishIfreview(1l);
		paramMap.put(webDish.key, webDish);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,10);
		paramMap.put("orderBy", "dish_when desc");
		List<CloudWebDish> dishList = cloudWebDishService.getListPage(paramMap);
		model.addAttribute("storePicList", dishList);
	}
	//获取最新的视频显示在首页
	private void indexVideo(ModelMap model){
		String porCode="3001";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebMessage webMessage = new CloudWebMessage();
		webMessage.setPorCode(porCode);
		webMessage.setIfDelete(0l);
		webMessage.setMesCatagory("video");
		webMessage.setMesStatus(1l);
		paramMap.put(webMessage.key, webMessage);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,1);
		paramMap.put("orderBy", "mes_status desc,mes_when desc");
		List<CloudWebMessage> list=cloudWebMessageService.getListPage(paramMap);
		if (null != list && list.size()>0) {
			CloudWebMessage msg=list.get(0);
			model.addAttribute("video",msg);
		}
	}
	//查询首页漂浮广告
	private void moveActivity(ModelMap model){
		String porCode="1101";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebMessage webMessage = new CloudWebMessage();
		webMessage.setPorCode(porCode);
		webMessage.setIfDelete(0l);
		webMessage.setMesIfreview(1l);//1 在首页显示
		paramMap.put(webMessage.key, webMessage);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,2);
		paramMap.put("orderBy", "mes_when desc");
		List<CloudWebMessage> list=cloudWebMessageService.getListPage(paramMap);
		model.addAttribute("moveList",list);
	}
	private void QQinfo(ModelMap model){
		Dict pre=dictService.getDictByCode("presalePhone");
		Dict suf=dictService.getDictByCode("sufsalePhone");
		Dict qq=dictService.getDictByCode("qq");
		model.put("pre", pre);
		model.put("suf", suf);
		model.put("qq", qq);
		
		String porCode="1102";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		CloudWebMessage webMessage = new CloudWebMessage();
		webMessage.setPorCode(porCode);
		webMessage.setIfDelete(0l);
		webMessage.setMesIfreview(1l);
		webMessage.setMesCatagory("qq");
		paramMap.put(webMessage.key, webMessage);
		paramMap.put(PAGE_NAME, 1);
		paramMap.put(PAGESIZE_NAME,1);
		paramMap.put("orderBy", "mes_status desc,mes_when desc");
		List<CloudWebMessage> newsList=cloudWebMessageService.getListPage(paramMap);
			if (newsList!=null && newsList.size()>0) {
				model.addAttribute("qqinfo",newsList.get(0));
			}
	}
	
}
